<template>
	<view class="page_box">
		<!-- 搜索框 -->
		<view class="head_box">
			<!-- <u-search v-model="searchVal" margin="30rpx" placeholder="搜索商品" height="64" @search="onSearch" @custom="onSearch" @clear="clearSearch"></u-search> -->
			<u-search placeholder="搜索信息" v-model="searchVal" margin="30rpx" @search="onSearch"  @custom="onSearch" @clear="clearSearch"></u-search>
		</view>
		
		<view class="content_box">
			<!-- 搜索历史 -->
			<view class="search-history" v-if="myFollowers.length">
				<view class="title-box u-flex u-row-between u-m-b-40">
					<view class="title">我的好友</view>
				</view>
				<view class="content u-flex-page u-col-center u-row-left u-flex-wrap">
					<uni-list v-for="(item, index) in myFollowers" :key="index" style="width: 100%;">
						<uni-list-item>
							<template v-slot:header>
								<view class="slot-box">
									<image class="slot-image" :src="item.avater" mode="widthFix" @click="tapAvater(item)"></image>
								</view>
							</template>
							<template v-slot:body>
								<text class="slot-box slot-text" @click="backPage(item)">
								{{item.username}}<text style="color:#fdbb03;">{{item.unionid=='1'?'@拾光坞':''}}</text>
								</text>
							</template>
							<template v-slot:footer>
								<text v-show="receiveMsgsCount[item.id] && receiveMsgsCount[item.id]>0 && receiveMsgsCount[item.id]<10" class="slot-box" style="width: 30rpx;height: 30rpx;
								border-radius: 20rpx;background-color: red;color: white;justify-content: center;font-size: 20rpx;">
									{{receiveMsgsCount[item.id]}}
								</text>
								<text v-show="receiveMsgsCount[item.id] && receiveMsgsCount[item.id]>9" class="slot-box" style="width: 18rpx;height: 18rpx;
								border-radius: 18rpx;background-color: red;color: white;justify-content: center;">
								</text>
								<!-- <view class="slot-box" style="width: 30rpx;height: 30rpx;
								border-radius: 20rpx;background-color: red;color: white;justify-content: center;font-size: 20rpx;">
									{{receiveMsgsCount[item.id]}}
								</view> -->
							</template>
						</uni-list-item>
					</uni-list>
					<!-- <uni-load-more :status="status" :content-text="contentText" v-if='state' style="width: 100%;" /> -->
					
				</view>
			</view>
			<uni-load-more :status="status" :content-text="contentText" v-if='state' style="width: 100%;" />
		</view>
		
		
		
	</view>
</template>

<script>
	import request from '@/common/request.js';
	export default {
		data() {
			return {
				searchVal: '',
				myFollowers: [],
				historyMessages: [],
				receiveMsgsCount: [],  //用于存储各个用户待接收的消息数量
				state:false,
				currPage: 1,
				status: 'more',
				ifMore: true,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				doSearch: false,
				//标记最近一次的操作是搜索还是查询关注者
				searchOrlistMyFollowers: 0,
			}
		},
		created() {
			//接收在线消息
			const _this = this;
			uni.$on('onlineMsgs', (msgs) => {
				msgs.forEach(msg => {
					if(!_this.historyMessages[msg.msg.userinfo.uid]) {
						_this.historyMessages[msg.msg.userinfo.uid] = [];
						_this.receiveMsgsCount[msg.msg.userinfo.uid] = 0;
					}
					_this.historyMessages[msg.msg.userinfo.uid].push(msg);
					_this.$set(_this.receiveMsgsCount, msg.msg.userinfo.uid, _this.receiveMsgsCount[msg.msg.userinfo.uid] + 1);
				})
				// _this.$forceUpdate();
				console.log("接收到在线消息数: " + _this.receiveMsgsCount[100])
			})
		},
		onReady() {
			//接收离线消息
			const _this = this;
			uni.$once('historyMsgs', (msgs) => {
				msgs.forEach(msg => {
					if(!_this.historyMessages[msg.msg.userinfo.uid]) {
						_this.historyMessages[msg.msg.userinfo.uid] = [];
						_this.receiveMsgsCount[msg.msg.userinfo.uid] = 0;
					}
					_this.historyMessages[msg.msg.userinfo.uid].push(msg);
					_this.$set(_this.receiveMsgsCount, msg.msg.userinfo.uid, _this.receiveMsgsCount[msg.msg.userinfo.uid] + 1);
				})
				console.log("接收到历史消息数: " + _this.receiveMsgsCount[100])
			})
			setTimeout(() => {
				_this.listMyFollowers();
			}, 500);
		},
		onReachBottom() {
			this.state = true
			//最近一次的操作是查询关注者，才继续翻页查询
			if(this.searchOrlistMyFollowers == 2){
				this.listMyFollowers()
			}
		},
		beforeDestroy() {
			uni.$off('onlineMsgs')
		},
		methods: {
			// 搜索用户名
			async onSearch(e) {
				if(!e.trim()){
					return
				}
				const _this = this;
				if(_this.doSearch){
					return
				}
				_this.doSearch = true;
				const res = await keywordSearchUser({
					keyword: e
				})
				if(res.data.code == 200){
					_this.myFollowers = []
					if(res.data.data.length>0){
						// console.info(res.data.data.records)
						_this.myFollowers = []
						_this.myFollowers = [..._this.myFollowers, ...res.data.data]
					}
				}
				_this.searchOrlistMyFollowers = 1
				_this.state = true
				_this.status = 'nomore'
				
				_this.doSearch = false
			},
			// 清除输入框
			clearSearch() {
				this.searchVal = '';
				this.state = false
				this.myFollowers = []
				this.currPage = 1
				this.listMyFollowers()
			},
			async listMyFollowers() {
				const _this = this;
				this.status = 'loading'
				if (!this.ifMore) {
					this.status = 'nomore'
					return false
				}
				uni.showNavigationBarLoading()
				this.status = 'loading'
				
				// const res = await listMyFollowers({
				// 	currPage: _this.currPage
				// })
				let opts = {
					url: '/business2/user/all',
					method: 'get'
				};
				let params = {currPage: _this.currPage}
				const res = await request.httpTokenRequest(opts,params)
				uni.hideNavigationBarLoading()
				_this.searchOrlistMyFollowers = 2;
				if(res.data){
					if(res.data.length>0){
						//去重
						if(_this.myFollowers.findIndex(item => item.id===res.data[0].id) == -1) {
							_this.myFollowers.push(...res.data)
							// //初始化this.historyMessages
							// res.data.forEach(item => {
							// 	if(!this.historyMessages[item.id]) {
							// 		this.historyMessages[item.id] = [];
							// 	}
							// })
						}
						_this.currPage = _this.currPage + 1;
						_this.status = 'more'
					}else{
						_this.status = 'nomore'
						_this.ifMore = false
					}
				}				
			},
			tapAvater(item){
				uni.navigateTo({
					url: '/pages/me/otherPage?userInfo='+JSON.stringify(item.userBasicInfoDTO)+"&isfollow="+item.follow
				});
				return false;
			},
			backPage(item){
				if(this.historyMessages[item.id]) {
					uni.setStorageSync('subHistoryMsgs' + item.id, this.historyMessages[item.id]);
					//同时将this.historyMessages[item.id]置空
					this.historyMessages[item.id] = []
					this.$set(this.receiveMsgsCount, item.id, 0);
				}
				uni.navigateTo({
					url: '/tn_components/chat/chat/chat?toUid=' + item.id
				});
			},
		}
	}
</script>

<style lang="scss">
	/**
	* 弹性盒三段式布局
	*/
	.page_box {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		overflow-x: hidden;
		// background: $u-bg-color;
	}
	.scroll-box {
		flex: 1;
		height: 100%;
		position: relative;
	}
	.content_box {
		flex: 1;
		overflow-y: auto;
	}
	
	
	.u-flex {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	
	.u-flex-page {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
	}
	
	.u-flex-wrap {
		flex-wrap: wrap;
	}
	
	.u-row-between {
		justify-content: space-between;
	}
	
	.page_box {
		background-color: #fff;
	}
	.search-history {
		padding: 30rpx;
		.title-box {
			.title {
				font-size: 30rpx;
				font-weight: bold;
				color: #333333;
				padding-bottom: 20rpx;
			}
			.clear-history-btn {
				font-size: 28rpx;
				font-weight: 500;
				color: #999999;
			}
		}
		.content {
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			.item {
				padding: 0 20rpx;
				line-height: 60rpx;
				background: #f5f6f8;
				border-radius: 30rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				max-width: 690rpx;
			}
			.history-item{
				margin:10rpx 10rpx;
			}
		}
	}
	
	.slot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
		
	.slot-image {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
		border-radius: 8rpx;
	}
		
	.slot-text {
		flex: 1;
		font-size: 14px;
	//	color: #4cd964;
		margin-right: 10px;
		align-items: center;
		text-align: center;
	}
</style>
